<?php

/*============================================
Classe SysPassword
============================================*/

class SysPassword extends ModelObject{

	protected static $cached_object_list= array();
	protected $USER_PASSWORD;
	protected $USER_OPERATOR_NAME;
	protected $USER_FREETEXT;
	protected $DB_LEVEL;
	protected $DB_RIGHTS;
	protected $USER_PRIVILEGE;
	protected $USER_PROFILE;
	protected $USER_ID_MODE;

	protected function __construct( $USER_PASSWORD,$USER_OPERATOR_NAME,$USER_FREETEXT,$DB_LEVEL,$DB_RIGHTS,$USER_PRIVILEGE,$USER_PROFILE,$USER_ID_MODE){
		try{
			
			$this->USER_PASSWORD = $USER_PASSWORD;
			$this->USER_OPERATOR_NAME = $USER_OPERATOR_NAME;
			$this->USER_FREETEXT = $USER_FREETEXT;
			$this->DB_LEVEL = $DB_LEVEL;
			$this->DB_RIGHTS = $DB_RIGHTS;
			$this->USER_PRIVILEGE = $USER_PRIVILEGE;
			$this->USER_PROFILE = $USER_PROFILE;
			$this->USER_ID_MODE = $USER_ID_MODE;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getUSERPASSWORD(){
		return $this->USER_PASSWORD;
	}

	public function getUSEROPERATORNAME(){
		return $this->USER_OPERATOR_NAME;
	}

	public function getUSERFREETEXT(){
		return $this->USER_FREETEXT;
	}

	public function getDBLEVEL(){
		return $this->DB_LEVEL;
	}

	public function getDBRIGHTS(){
		return $this->DB_RIGHTS;
	}

	public function getUSERPRIVILEGE(){
		return $this->USER_PRIVILEGE;
	}

	public function getUSERPROFILE(){
		return $this->USER_PROFILE;
	}

	public function getUSERIDMODE(){
		return $this->USER_ID_MODE;
	}

	public function setUSERPASSWORD( $USER_PASSWORD){
		try{
			$temp = $this->USER_PASSWORD;
			if($this->USER_PASSWORD !== $USER_PASSWORD){
				$this->USER_PASSWORD = $USER_PASSWORD;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->USER_PASSWORD = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setUSERPASSWORD() a échouée",0,$e);
		}
	}

	public function setUSERFREETEXT( $USER_FREETEXT){
		try{
			$temp = $this->USER_FREETEXT;
			if($this->USER_FREETEXT !== $USER_FREETEXT){
				$this->USER_FREETEXT = $USER_FREETEXT;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->USER_FREETEXT = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setUSERFREETEXT() a échouée",0,$e);
		}
	}

	public function setDBLEVEL( $DB_LEVEL){
		try{
			$temp = $this->DB_LEVEL;
			if($this->DB_LEVEL !== $DB_LEVEL){
				$this->DB_LEVEL = $DB_LEVEL;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->DB_LEVEL = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDBLEVEL() a échouée",0,$e);
		}
	}

	public function setDBRIGHTS( $DB_RIGHTS){
		try{
			$temp = $this->DB_RIGHTS;
			if($this->DB_RIGHTS !== $DB_RIGHTS){
				$this->DB_RIGHTS = $DB_RIGHTS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->DB_RIGHTS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDBRIGHTS() a échouée",0,$e);
		}
	}

	public function setUSERPRIVILEGE( $USER_PRIVILEGE){
		try{
			$temp = $this->USER_PRIVILEGE;
			if($this->USER_PRIVILEGE !== $USER_PRIVILEGE){
				$this->USER_PRIVILEGE = $USER_PRIVILEGE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->USER_PRIVILEGE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setUSERPRIVILEGE() a échouée",0,$e);
		}
	}

	public function setUSERPROFILE( $USER_PROFILE){
		try{
			$temp = $this->USER_PROFILE;
			if($this->USER_PROFILE !== $USER_PROFILE){
				$this->USER_PROFILE = $USER_PROFILE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->USER_PROFILE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setUSERPROFILE() a échouée",0,$e);
		}
	}

	public function setUSERIDMODE( $USER_ID_MODE){
		try{
			$temp = $this->USER_ID_MODE;
			if($this->USER_ID_MODE !== $USER_ID_MODE){
				$this->USER_ID_MODE = $USER_ID_MODE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->USER_ID_MODE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setUSERIDMODE() a échouée",0,$e);
		}
	}

	public static function create( $USER_PASSWORD,$USER_OPERATOR_NAME,$USER_FREETEXT,$DB_LEVEL,$DB_RIGHTS,$USER_PRIVILEGE,$USER_PROFILE,$USER_ID_MODE){
		try{
			$sysPassword = new SysPassword($USER_PASSWORD, $USER_OPERATOR_NAME, $USER_FREETEXT, $DB_LEVEL, $DB_RIGHTS, $USER_PRIVILEGE, $USER_PROFILE, $USER_ID_MODE);
			$sysPassword->insert();
			SysPassword::cacheObject($sysPassword);
			return $sysPassword;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type SysPassword a échouée",0,$e);
		}
	}

	public static function get( $USER_OPERATOR_NAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($USER_OPERATOR_NAME) || $USER_OPERATOR_NAME === "") {
				return null;
			}
			if(SysPassword::existsInCache($USER_OPERATOR_NAME)) {
				return SysPassword::getFromCache($USER_OPERATOR_NAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `sys_password` WHERE `USER_OPERATOR_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$USER_OPERATOR_NAME));
			if ($row = $stmt->fetch()){
				$sysPassword = new SysPassword( $row['USER_PASSWORD'],$row['USER_OPERATOR_NAME'],$row['USER_FREETEXT'],$row['DB_LEVEL'],$row['DB_RIGHTS'],$row['USER_PRIVILEGE'],$row['USER_PROFILE'],$row['USER_ID_MODE']);
				$sysPassword->id =  $row['USER_OPERATOR_NAME'];
				$dbh->commit();
				SysPassword::cacheObject($sysPassword);
				return $sysPassword;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $USER_OPERATOR_NAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `sys_password` SET `USER_PASSWORD` = :USER_PASSWORD, `USER_FREETEXT` = :USER_FREETEXT, `DB_LEVEL` = :DB_LEVEL, `DB_RIGHTS` = :DB_RIGHTS, `USER_PRIVILEGE` = :USER_PRIVILEGE, `USER_PROFILE` = :USER_PROFILE, `USER_ID_MODE` = :USER_ID_MODE WHERE `USER_OPERATOR_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':USER_PASSWORD'=>$this->USER_PASSWORD, ':USER_FREETEXT'=>$this->USER_FREETEXT, ':DB_LEVEL'=>$this->DB_LEVEL, ':DB_RIGHTS'=>$this->DB_RIGHTS, ':USER_PRIVILEGE'=>$this->USER_PRIVILEGE, ':USER_PROFILE'=>$this->USER_PROFILE, ':USER_ID_MODE'=>$this->USER_ID_MODE, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `sys_password` WHERE `USER_OPERATOR_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			SysPassword::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `sys_password` ( `USER_PASSWORD`, `USER_FREETEXT`, `DB_LEVEL`, `DB_RIGHTS`, `USER_PRIVILEGE`, `USER_PROFILE`, `USER_ID_MODE`) VALUES (:USER_PASSWORD, :USER_FREETEXT, :DB_LEVEL, :DB_RIGHTS, :USER_PRIVILEGE, :USER_PROFILE, :USER_ID_MODE)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':USER_PASSWORD'=>$this->USER_PASSWORD, ':USER_FREETEXT'=>$this->USER_FREETEXT, ':DB_LEVEL'=>$this->DB_LEVEL, ':DB_RIGHTS'=>$this->DB_RIGHTS, ':USER_PRIVILEGE'=>$this->USER_PRIVILEGE, ':USER_PROFILE'=>$this->USER_PROFILE, ':USER_ID_MODE'=>$this->USER_ID_MODE));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `sys_password`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(SysPassword::existsInCache($row['USER_OPERATOR_NAME'])){
					$obj = SysPassword::getFromCache($row['USER_OPERATOR_NAME']);
				}
				else{
					$obj = new SysPassword( $row['USER_PASSWORD'],$row['USER_OPERATOR_NAME'],$row['USER_FREETEXT'],$row['DB_LEVEL'],$row['DB_RIGHTS'],$row['USER_PRIVILEGE'],$row['USER_PROFILE'],$row['USER_ID_MODE']);
				}
				if(!is_null($obj)){
					$obj->id = $row['USER_OPERATOR_NAME'];
					SysPassword::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type SysPassword a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `sys_password`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type SysPassword a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `sys_password`';
			$sql_select = 'SELECT USER_OPERATOR_NAME FROM `sys_password`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = SysPassword::getObjectFromCache($row['USER_OPERATOR_NAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						SysPassword::removeObjectFromCache($row['USER_OPERATOR_NAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type SysPassword a échouée",0,$e);
		}
	}

}